package com.example.yorun.asmallproject;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
import com.nostra13.universalimageloader.core.imageaware.ViewAware;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

/**
 * 测试 uil 加载同一个url给两个不同控件的bug
 */
public class MainActivity extends AppCompatActivity {

    String url = "https://pic.cnblogs.com/face/u20388.jpg";
    private ImageView iv1;
    private ImageView iv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv1 = findViewById(R.id.iv1);
        iv2 = findViewById(R.id.iv2);
        Utils.init(getApplication());
    }

    public void 加载(View view) {
        load(iv1,url);
        load(iv2,url);
    }
    void loadByGlide(final ImageView iv, final String url) {
        iv.setImageDrawable(null);
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                Glide.get(MainActivity.this).clearDiskCache();
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                Glide.get(MainActivity.this).clearMemory();
                Glide.with(MainActivity.this).load(url).into(iv);
            }
        }.execute();
    }


    void load(final ImageView iv, String url) {
        ImageLoader.getInstance().getMemoryCache().clear();
        ImageLoader.getInstance().getDiskCache().clear();
        iv.setImageDrawable(null);
        ImageLoader.getInstance().displayImage(url, new ViewAware(iv) {
            @Override
            protected void setImageDrawableInto(Drawable drawable, View view) {
                Yr.d();
            }
            @Override
            protected void setImageBitmapInto(Bitmap bitmap, View view) {
                Yr.d();
            }
        }, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                Yr.d();
            }
            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                Yr.d();
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                Yr.d(view);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                Yr.d();
            }
        });
//        ImageLoader.getInstance().loadImage(url, new ImageLoadingListener() {
//            @Override
//            public void onLoadingStarted(String imageUri, View view) {
//                Yr.d();
//            }
//
//            @Override
//            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
//                Yr.d();
//            }
//
//            @Override
//            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//                iv.setImageBitmap(loadedImage);
//                Yr.d();
//            }
//
//            @Override
//            public void onLoadingCancelled(String imageUri, View view) {
//                Yr.d();
//            }
//        });
    }

    public void Glide加载(View view) {
        loadByGlide(iv1,url);
        loadByGlide(iv2,url);
    }
}
